//
//  StringEx.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/19.
//

import Foundation

extension String {
    func momentDay() -> Int {
        return Calendar.current.component(.day, from: self.moment())
    }
    
    func momentMonth() -> Int {
        return Calendar.current.component(.month, from: self.moment())
    }
    
    func momentQuarter() -> Int {
        return Date.getCurrentQuarter(month: Calendar.current.component(.month, from: self.moment()))
    }
    
    func momentYear() -> Int {
        return Calendar.current.component(.year, from: self.moment())
    }
    
    func moment() -> Date {
        let fmt = DateFormatter()
        fmt.dateFormat = "yyyy-MM-dd"
        fmt.timeZone = TimeZone.current
        let date = fmt.date(from: self)
        return date ?? Date()
    }

    var html2AttributedString: NSAttributedString?  {
        do {
            return try NSAttributedString(data: Data(utf8),
                                          options: [.documentType: NSAttributedString.DocumentType.html,
                                                    .characterEncoding: String.Encoding.utf8.rawValue],
                                          documentAttributes: nil)
        } catch {
            return nil
        }
    }
    
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
